using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Unity.VisualScripting;

public class GameManager : MonoBehaviour
{
    public List<GameObject> targets;
    private float spawnRate=1f;
    public TMP_Text scoreText;
    public int score=0;
    //public TMP_Text gameOverText;
    public GameObject gameOverPanel;
    public Button restartButton;
    private bool gameOver = false;
    public List<Button> difficultyButtons;
    public GameObject difficultyButtonsPanel;

    // Start is called before the first frame update
    void Start()
    {
        //StartCoroutine(SpawnTarget());
        scoreText.text= "Score:" + score;
        restartButton.onClick.AddListener(RestartGame);
        //for(int i=0;i<difficultyButtons.Count;i++)
        //{
        //    difficultyButtons[i].onClick.AddListener(SetDifficulty);
        //}
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void StartGame(int difficulty)
    {
        difficultyButtonsPanel.gameObject.SetActive(false);
        spawnRate = 1f / difficulty;
        StartCoroutine(SpawnTarget());
    }

    IEnumerator SpawnTarget()
    {
        while(!gameOver)
        {
            yield return new WaitForSeconds(spawnRate);
            int index = Random.Range(0, targets.Count);
            Instantiate(targets[index]);
        }      
    }

    public void UpdateScore(int scoreToAdd)
    {
       score+=scoreToAdd;
       scoreText.text = "Score:" + score;
    }

    public void GameOver()
    {
        gameOverPanel.gameObject.SetActive(true);
        gameOver = true;
    }

    public void RestartGame()
    {
        SceneManager.LoadScene("SampleScene");
    }

}
